package com.sinosoft.data1;

import java.math.BigDecimal;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.sinosoft.base.Constants;
import com.sinosoft.base.GenericDaoHibernate;
import com.sinosoft.product.ZywxImage1;
import com.sinosoft.systemConfig.SystemConfig;
import com.sinosoft.util.DateTimeUtils;
import com.sinosoft.util.SystemLogUtil;


@Repository
public class ZywxImage1DaoHibernate extends GenericDaoHibernate<ZywxImage1, Long> implements ZywxImage1Dao  {

	public ZywxImage1DaoHibernate() {
		super(ZywxImage1.class);
		// TODO Auto-generated constructor stub
	}

	public int getPageSize(String sql) {
			String strsql = "select count(*) from ZYWX_IMAGE_1  " + sql;
			int pageCount = 0;
			Session session = null;
			try {
				session = this.getHibernateTemplate().getSessionFactory().openSession();
				Query query = session.createSQLQuery(strsql);
				List list = query.list();
			
				pageCount = Integer.valueOf(list.get(0).toString());
			} catch (Exception e) {
				SystemLogUtil.saveError(Constants.LOG_ERROR, e.getMessage());
			} finally {
				session.close();
			}
            return pageCount;
		
	}

	public List<ZywxImage1> getZywxImage1() {
		
		return getHibernateTemplate().find("from ZywxImage1 zywx1");
	}

	public ZywxImage1 getZywxImage1ById(long productid) {
		List zywxlist = getHibernateTemplate().find("from ZywxImage1 where productid=?", productid);
		if (zywxlist == null || zywxlist.isEmpty()) {
			return null;
		} else {
			return (ZywxImage1) zywxlist.get(0);
		}
		
	}

	public ZywxImage1 getZywxImage1BySensorid(String sensorid) {
		
		String hqls = "from ZywxImage1 where sensorid='"+sensorid+"'";
		List<ZywxImage1> ZywxImage1list = this.getHibernateTemplate().find(hqls);
		return ZywxImage1list!=null&&ZywxImage1list.size()>0?ZywxImage1list.get(0):null;
		
	}
	//查询所有信息
	public List<ZywxImage1> getZywxImage1List(int pageFirst, int pageSize) {
		List<ZywxImage1> list = null;
		Session session = null;
		try {
			session = this.getHibernateTemplate().getSessionFactory()
					.openSession();
			Criteria c = session.createCriteria(ZywxImage1.class);
			c.setFirstResult(pageFirst);
			c.setMaxResults(pageSize);
			list = c.list();

		} catch (Exception e) {
			SystemLogUtil.saveError(Constants.LOG_ERROR, e.getMessage());
		} finally {
			session.close();

		}
		return list;
	}
	//通过条件查询列表
	public List<ZywxImage1> getZywxImage1List(int pageFirst, int pageSize,
			ZywxImage1 zywx1) {
		List<ZywxImage1> list= null;
		String Sen = zywx1.getSensorid();
		Session session = null;
		Criteria c = null;
		try {
			session = this.getHibernateTemplate().getSessionFactory()
					.openSession();
			c = session.createCriteria(ZywxImage1.class);
			if (!"".equalsIgnoreCase(Sen)) {
				c.add(Restrictions.like("sceneid", "%" + Sen + "%"));
			}

			c.setFirstResult(pageFirst);
			c.setMaxResults(pageSize);
			list = c.list();

		} catch (Exception e) {
			SystemLogUtil.saveError(Constants.LOG_ERROR, e.getMessage());
		} finally {
			session.close();

		}

		return list;
	}

	public void removeZywxImage1(String productid) {
		ZywxImage1 zywx = new ZywxImage1();
		List zywxlist = getHibernateTemplate().find(
				"from ZywxImage1 where productid=?", productid);
		if (zywxlist == null || zywxlist.isEmpty()) {

		} else {
			zywx = (ZywxImage1) zywxlist.get(0);
		}

		getHibernateTemplate().delete(zywx);
		getHibernateTemplate().flush();
		
	}
	//添加或修改的方法
	public ZywxImage1 saveZywxImage1(ZywxImage1 zywx1) {
		if (this.getZywxImage1Bythree(zywx1.getProductid(), zywx1.getSensorid(), zywx1.getSatelliteid())==0)
			getHibernateTemplate().save(zywx1);
		else 
			getHibernateTemplate().merge(zywx1);//merge是强制修改
		getHibernateTemplate().flush();
	
		return zywx1;
	}

	public ZywxImage1 getZywxImage1ById(String productid) {
		List zywxlist = getHibernateTemplate().find(
				"from ZywxImage1 where productid=?", productid);
		if(zywxlist == null || zywxlist.isEmpty()){
			return null;
		}else{
			return (ZywxImage1) zywxlist.get(0);
		}
		
	}
	//联合主键通过这3个字段查出一条记录  返回0是增加，返回1是修改
	public int getZywxImage1Bythree(String productid,String sensorid,String satelliteid) {
		List zywxlist = getHibernateTemplate().find(
				"from ZywxImage1 where productid=? and  sensorid=? and satelliteid =?", productid,sensorid,satelliteid);
		if(zywxlist == null || zywxlist.isEmpty()){
			return 0;
		}else{
			return 1;
		}
	}
}
